package com.gxa.dao.impl;

import com.gxa.common.Constants;
import com.gxa.dao.CustomerValueDao;
import com.gxa.dbutils.JdbcBuild;
import com.gxa.pojo.Customer;
import com.gxa.pojo.Orders;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;

public class CustomerValueDaoImpl extends JdbcBuild implements CustomerValueDao {
    /**
     * 查询客户信息
     * @param currentPage
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public List queryCustomer(Integer currentPage, Integer pageSize) throws SQLException {
        String sql = "select * from customer where del_status=? limit ?,?";
        Object[] params = {Constants.STATUS_OK,currentPage, pageSize};
        List<Customer> list = super.queryListEntity(Customer.class, sql, params);
        return list;
    }

    /**
     * 查询指定客户的所有订单信息
     * @param customer_name
     * @return
     * @throws SQLException
     */
    @Override
    public List queryOrder(String customer_name) throws SQLException {
        String sql = "select * from orders where name=?";
        Object[] params = {customer_name};
        List<Orders> list = super.queryListEntity(Orders.class, sql, params);
        return list;
    }

    /**
     * 统计指定客户的总消费金额
     * @param customer_name
     * @return
     * @throws SQLException
     */
    @Override
    public BigDecimal getConsumeTotalMoney(String customer_name) throws SQLException {
        String sql = "select sum(money*num) as consume_total_money from orders where name=?";
        Object[] params = {customer_name};
        Customer customer = super.queryEntity(Customer.class, sql, params);
        BigDecimal consumeTotalMoney = customer.getConsume_total_money();
        return consumeTotalMoney;
    }

    /**
     * 按条件搜索客户信息
     * @param currentPage
     * @param pageSize
     * @param customer_name
     * @param type
     * @return
     * @throws SQLException
     */
    @Override
    public List searchCustomer(Integer currentPage, Integer pageSize, String customer_name, String type) throws SQLException {
//        System.out.println("customer_name:"+customer_name+" type:"+type);
        String sql = null;
        List<Customer> list = null;
        if(customer_name!=null&&type!=null){
            sql = "select * from customer where customer_name=? and type=? and del_status=? limit ?,?";
            Object[] params = {customer_name, type,Constants.STATUS_OK, currentPage, pageSize};
            list = super.queryListEntity(Customer.class, sql, params);
        }else if(customer_name==null&&type!=null){
            sql = "select * from customer where type=? and del_status=? limit ?,?";
            Object[] params = {type,Constants.STATUS_OK, currentPage, pageSize};
            list = super.queryListEntity(Customer.class, sql, params);
        }else if(customer_name!=null&&type==null){
            sql = "select * from customer where customer_name=? and del_status=? limit ?,?";
            Object[] params = {customer_name,Constants.STATUS_OK, currentPage, pageSize};
            list = super.queryListEntity(Customer.class, sql, params);
        }else if(customer_name==null&&type==null){
            sql = "select * from customer where del_status=? limit ?,?";
            Object[] params = {Constants.STATUS_OK,currentPage, pageSize};
            list = super.queryListEntity(Customer.class, sql, params);
        }
        return list;
    }

    /**
     * 查询指定客户的所有订单信息
     * @param currentPage
     * @param pageSize
     * @param customer_name
     * @return
     * @throws SQLException
     */
    @Override
    public List queryCustomerOrder(Integer currentPage, Integer pageSize, String customer_name) throws SQLException {
        String sql = "select * from orders where name=? limit ?,?";
        Object[] params = {customer_name,currentPage,pageSize};
        List<Orders> orders = super.queryListEntity(Orders.class, sql, params);
        return orders;
    }
}
